package com.unhz.taigu.service.impl;

import com.unhz.generator.dao.SysPermissionMapper;
import com.unhz.generator.dao.SysRolePermissionMapper;
import com.unhz.generator.pojo.SysPermission;
import com.unhz.generator.pojo.SysRolePermission;
import com.unhz.generator.utils.EmptyUtil;
import com.unhz.generator.utils.IsNumeric;
import com.unhz.generator.utils.Page;
import com.unhz.taigu.service.SysPermissionService;
import com.unhz.taigu.service.permission.SysPermissionCustomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by cxg on 2017/11/13 0013.
 *
 * @Author 曹显贵
 * @Date 2017/11/13 0013 13:49
 */
@Service
public class SysPermissionServiceImpl implements SysPermissionService {
    static final int PAGENUM = 1;
    @Autowired
    private SysPermissionMapper sysPermissionMapper;
    @Autowired
    private SysRolePermissionMapper sysRolePermissionMapper;

    @Autowired
    private SysPermissionCustomService sysPermissionCustomService;


    @Override
    public List<SysPermission> selectAllPermission() {
        return sysPermissionMapper.selectAllPermission();
    }

    @Override
    public Page<SysPermission> selectPagePermission(String pageNo, Integer pageSize, String keywords) {
        int pageNum = PAGENUM;
        boolean numok = IsNumeric.isNumeric(pageNo);
        if(numok){
            if(pageNo !=null && !"".equals(pageNo)){
                pageNum = Integer.parseInt((pageNo));
            }
        }
        int totalRecords = sysPermissionMapper.getTotalRecordsByKeywords(keywords);        //获取记录总条数
        Page page = new Page(pageNum,totalRecords);
        page.setPageSize(pageSize);
        Map<String ,Object> map = new HashMap<String,Object>();
        map.put("startIndex",page.getStartIndex());
        map.put("pageSize",page.getPageSize());
        map.put("keywords",keywords);
        //获取总记录数
        List<SysPermission> records = sysPermissionMapper.findPermissionMap(map);
        page.setRecords(records);
        String url = "/permissionController/findAllPermission?keywords="+keywords;
        page.setUri(url);
        return page;
    }

    @Override
    public int deleteByPermissionId(Integer permissionId) {
        //删除role_Permission
        int count = 0;
        //查询是否有数据
        List<SysRolePermission> sysRolePermissionList = sysRolePermissionMapper.selectByPermissionId(permissionId);
        if(!EmptyUtil.isEmpty(sysRolePermissionList)){
            count = sysRolePermissionMapper.deleteByPermissionId(permissionId);
            count = sysPermissionMapper.deleteByPrimaryKey(permissionId);
        }else{
            count = sysPermissionMapper.deleteByPrimaryKey(permissionId);
        }
        return count;
    }

    @Override
    public List<SysPermission> findAllParent() {
        return sysPermissionMapper.findAllParent();
    }

    @Override
    public String insertPermission(SysPermission sysPermission) {
        sysPermission.setIsDelete("0");
        int count = sysPermissionMapper.insertSelective(sysPermission);
        if(count > 0){
            return "success";
        }
        return "false";
    }

    @Override
    public SysPermission selectByPermissionId(Integer permissionId) {
        return sysPermissionMapper.selectByPrimaryKey(permissionId);
    }

    @Override
    public String updatePermission(SysPermission sysPermission) {
        int count = sysPermissionMapper.updateByPrimaryKeySelective(sysPermission);
        if(count > 0){
            return "success";
        }
        return "false";
    }

    @Override
    public List<SysPermission> findAllChild() {
        return sysPermissionMapper.selectAllChild();
    }

    @Override
    public List<SysPermission> findAllPermission() {
        return sysPermissionMapper.findAllPermission();
    }

    @Override
    public List<SysPermission> findAllBig() {
        return sysPermissionMapper.findAllBig();
    }
    @Override
    public List<SysPermission> selectPermission() {
        return sysPermissionMapper.findAllPermission();
    }

    @Override
    public List<SysPermission> selectMenus() {
        return sysPermissionMapper.selectAllChild();
    }

    @Override
    public List<SysPermission> selectPermissionByRoleId(Integer roleId) {
        return sysPermissionCustomService.findPermissionByRoleId(roleId);
    }

    @Override
    public List<SysPermission> selectMenusByRoleId(Integer roleId) {
        return sysPermissionCustomService.findMenuByRoleId(roleId);
    }


}
